package cn.com.cifi.mars.util;

import cn.com.cifi.mars.bean.vo.price.PriceApprovalDifferencesVo;
import cn.com.cifi.mars.entity.PriceApprovalDifferences;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 类名称：NewAllGoodsValueReviewTemplate
 * 类描述：货值审批-全盘货值差异概览模板
 *
 * @author yanboliu
 * @version 1.0.0
 */
@Slf4j
public class NewAllGoodsValueReviewTemplate {
    /**
     * 表格模板地址
     */
    private static final String TEMPLATE_PATH = "/template/new_all_goods_value_review_template.xlsx";
    /**
     * 初始化文件
     */
    private XSSFWorkbook workbook;
    /**
     * 初始化表格
     */
    private XSSFSheet sheet;
    /**
     * 默认sheet索引
     */
    private int defaultSheetIndex = 0;
    /**
     * 数据开始行
     */
    private int dataBeginRow = 2;
    /**
     * 当前行标
     */
    private int currentDataRowCursor = dataBeginRow;
    /**
     * 单元格样式
     */
    private XSSFCellStyle cellStyle;
    /**
     * 文字样式1
     */
    private XSSFCellStyle cellStyle1;
    /**
     * 文字样式2
     */
    private XSSFCellStyle cellStyle2;

    /**
     * 设置表格基本属性
     */
    public NewAllGoodsValueReviewTemplate(String name) {

        try {
            InputStream tempInputStream = NewAllGoodsValueReviewTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                XSSFRow row = sheet.getRow(dataBeginRow);
                cellStyle1 = sheet.getRow(3).getCell(2).getCellStyle();
                cellStyle2 = sheet.getRow(3).getCell(5).getCellStyle();

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量插入行
     *
     * @param priceApprovalDifferencesVo
     * @param sheetIndex
     */
    public synchronized void appendRows(PriceApprovalDifferencesVo priceApprovalDifferencesVo, int sheetIndex) {

        sheet = workbook.getSheetAt(sheetIndex);
        this.currentDataRowCursor = this.dataBeginRow;
        if (priceApprovalDifferencesVo.getAfterContent() != null) {
            cellStyle1 = sheet.getRow(3).getCell(2).getCellStyle();
            cellStyle2 = sheet.getRow(3).getCell(5).getCellStyle();
            appendRow(priceApprovalDifferencesVo.getAfterContent());

        } else {
            currentDataRowCursor++;
        }
        if (priceApprovalDifferencesVo.getBeforeContent() != null) {
            cellStyle1 = sheet.getRow(3).getCell(2).getCellStyle();
            cellStyle2 = sheet.getRow(3).getCell(5).getCellStyle();
            appendRow(priceApprovalDifferencesVo.getBeforeContent());
        } else {
            currentDataRowCursor++;
        }
        if (priceApprovalDifferencesVo.getDifferenceValueList() != null) {
            cellStyle1 = sheet.getRow(3).getCell(2).getCellStyle();
            cellStyle2 = sheet.getRow(3).getCell(5).getCellStyle();
            appendRow(priceApprovalDifferencesVo.getDifferenceValueList());
        } else {
            currentDataRowCursor++;
        }
        if (priceApprovalDifferencesVo.getDifferenceRateList() != null) {
            cellStyle1=sheet.getRow(5).getCell(1).getCellStyle();
            cellStyle2=sheet.getRow(5).getCell(1).getCellStyle();
            appendRow(priceApprovalDifferencesVo.getDifferenceRateList());
        } else {
            currentDataRowCursor++;
        }
    }

    /**
     * 插入行
     *
     * @param priceApprovalDifferences
     */
    public synchronized void appendRow(PriceApprovalDifferences priceApprovalDifferences) {
        String divisionSign = "%";
        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        int index = 1;
        //项目信息
        cellStyle = cellStyle1;
        this.appendCell(row, index++, priceApprovalDifferences.getTotalValue());
        this.appendCell(row, index++, priceApprovalDifferences.getQuityValue());
        this.appendCell(row, index++, priceApprovalDifferences.getHouseValue());
        this.appendCell(row, index++, priceApprovalDifferences.getHouseAvg());
        cellStyle = cellStyle2;
        if (StringUtils.isEmpty(priceApprovalDifferences.getHouseArea()) || priceApprovalDifferences.getHouseArea().endsWith(divisionSign)) {
            this.appendCell(row, index++, priceApprovalDifferences.getHouseArea());
        } else {
            this.appendCell(row, index++, String.valueOf(Double.parseDouble(priceApprovalDifferences.getHouseArea())));
        }
        cellStyle = cellStyle1;
        this.appendCell(row, index++, priceApprovalDifferences.getHouseNum());
        this.appendCell(row, index++, priceApprovalDifferences.getParkingValue());
        this.appendCell(row, index++, priceApprovalDifferences.getParkingAvg());
        this.appendCell(row, index++, priceApprovalDifferences.getParkingNum());
        this.appendCell(row, index++, priceApprovalDifferences.getForsaleValue());
        this.appendCell(row, index++, priceApprovalDifferences.getForsaleNum());
        currentDataRowCursor++;

    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, String value) {
        String divisionSign = "%";
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        if (StringUtils.isNotBlank(value) && value.endsWith(divisionSign)) {
            cell.setCellValue(value == null ? "" : value);
        } else {
            ReportUtils.setCellValue(cell, value == null ? "" : value);
        }
    }

    /**
     * 导出
     *
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}
